package cn.edu.imau.zy.system.controller;

import cn.edu.imau.zy.common.annotation.LoginUser;
import cn.edu.imau.zy.common.core.controller.BaseController;
import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.entity.student.StStudent;
import cn.edu.imau.zy.common.core.entity.system.SysClasses;
import cn.edu.imau.zy.common.core.entity.system.SysDept;
import cn.edu.imau.zy.common.core.entity.system.SysGradeYear;
import cn.edu.imau.zy.common.core.entity.system.SysUser;
import cn.edu.imau.zy.system.service.ISysClassesService;
import cn.edu.imau.zy.system.service.ISysDeptService;
import cn.edu.imau.zy.system.service.ISysGradeYearService;
import cn.hutool.core.util.IdUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 班级 提供者
 * 
 * @author wyz
 * @date 2020-06-19
 */
@RestController
@RequestMapping("classes")
public class SysClassesController extends BaseController
{
	
	@Autowired
	private ISysClassesService sysClassesService;
    @Autowired
    private ISysGradeYearService yearService;
	
	/**
	 * 查询${tableComment}
	 */
	@GetMapping("get/{classesId}")
	public SysClasses get(@PathVariable("classesId") Long classesId)
	{
		return sysClassesService.selectSysClassesById(classesId);
		
	}
	
	/**
	 * 查询班级列表-分页
	 */
	@GetMapping("list")
	public R list(@LoginUser SysUser user, SysClasses sysClasses)
	{
		startPage();
		if(user.getRoleIds().contains(4L)){
			sysClasses.setDirectorId(user.getUserId());
		}
        return result(sysClassesService.selectSysClassesList(sysClasses));
	}

    /**
     * 获取近年的班级树
     */
    @GetMapping("treeForGreadDept")
    public R list() {
        List<Long> yearList = yearService.selectSysGradeYearList(new SysGradeYear()).stream().map(p -> p.getYear()).collect(Collectors.toList());
        List<SysClasses> classesList = sysClassesService.selectSysClassesList(new SysClasses()).stream().filter(p -> yearList.contains(p.getGrade())).collect(Collectors.toList());
        List<TreeNode> list = classesList.stream().map(p -> new TreeNode(p.getClassesId(), "d" + p.getDeptId().toString() + "g" + p.getGrade().toString(), p.getClassesName())).collect(Collectors.toList());
        list.addAll(classesList.stream().map(p -> new TreeNode("d" + p.getDeptId().toString(), "0", p.getDeptName())).distinct().collect(Collectors.toList()));
        list.addAll(classesList.stream().map(p -> new TreeNode("d" + p.getDeptId().toString() + "g" + p.getGrade().toString(), "d" + p.getDeptId().toString(), p.getGrade().toString() + "级")).distinct().collect(Collectors.toList()));
        return R.data(list);
    }

    @Data
    class TreeNode {
        public TreeNode(String id, String parentId, String name) {
            this.id = id;
            this.parentId = parentId;
            this.name = name;
        }

        String id;
        String parentId;
        String name;
    }

	/**
	 * 查询班级列表
	 */
	@GetMapping("jlist")
    public List<SysClasses> jList(@LoginUser SysUser user, SysClasses sysClasses) throws Exception {
        if (user.getRoleIds().contains(1L) || user.getRoleIds().contains(7L)) {
            //管理员 学生处 查看所有
        } else if (user.getRoleIds().contains(6L)) {
            //系领导 查看本系
            sysClasses.setDeptId(user.getDeptId());
        } else if (user.getRoleIds().contains(4L)) {
            sysClasses.setDirectorId(user.getUserId());
        } else {
            throw new Exception("身份异常");
        }
		return sysClassesService.selectSysClassesList(sysClasses);
	}
	
	
	/**
	 * 新增保存班级
	 */
	@PostMapping("save")
	public R addSave(@RequestBody SysClasses sysClasses)
	{
		sysClasses.setClassesId(IdUtil.getSnowflake(1,1).nextIdStr());
		sysClasses.setCreateBy(String.valueOf(getCurrentUserId()));
		return toAjax(sysClassesService.insertSysClasses(sysClasses));
	}

    /**
     * 批量保存班级
     */
    @PostMapping("saveList")
    public R saveList(@RequestBody List<SysClasses> classes) {
        return toAjax(sysClassesService.insertSysClassesList(classes));
    }

	/**
	 * 修改保存班级
	 */
	@PostMapping("update")
	public R editSave(@RequestBody SysClasses sysClasses)
	{		
		return toAjax(sysClassesService.updateSysClasses(sysClasses));
	}
	
	/**
	 * 删除${tableComment}
	 */
	@PostMapping("remove")
	public R remove(String ids)
	{		
		return toAjax(sysClassesService.deleteSysClassesByIds(ids));
	}
}
